package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.*;
import com.sg.entity.OmsTargetCategoryExtend;
import com.sg.service.base.app.MOmsTargetCategoryExtendService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsTargetCategoryExtendMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsTargetCategoryExtendServiceImpl
        extends ServiceImpl<OmsTargetCategoryExtendMapper, OmsTargetCategoryExtend>
        implements MOmsTargetCategoryExtendService {

    @Autowired
    OmsTargetCategoryExtendMapper omsTargetCategoryExtendMapper;

    /**
     * 3-3-02-01查询目标分类扩展设置详情[3294]
     * gen by moon at 3/30/2023, 12:39:24 PM
     */
    @Trace(operationName = "3-3-02-01查询目标分类扩展设置详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTargetCategoryExtend queryTargetCatExSetDetail(QueryTargetCatExSetDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsTargetCategoryExtend> queryWrapper = new QueryWrapper<>();

            // TODO: 2023/5/29 手写右模糊代码
            if (reqDto.getTargetCategoryCode() != null) {
                queryWrapper.likeRight("target_category_code", reqDto.getTargetCategoryCode());
                reqDto.setTargetCategoryCode(null);
            }
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsTargetCategoryExtend> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsTargetCategoryExtend retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-02-01删除目标分类扩展设置[3295]
     * gen by moon at 10/15/2022, 10:05:56 PM
     */
    @Trace(operationName = "3-3-02-01删除目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTargetCatExSet(String objectivePlanningExtendId) {

        return super.removeById(objectivePlanningExtendId);
    }

    /**
     * 3-3-02-01新增目标分类扩展设置[3334]
     * gen by moon at 10/15/2022, 10:05:56 PM
     */
    @Trace(operationName = "3-3-02-01新增目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addTargetCatExSet(OmsTargetCategoryExtend omsTargetCategoryExtend) {
        omsTargetCategoryExtend.setObjectivePlanningExtendId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTargetCategoryExtend.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsTargetCategoryExtend.setIsUpdate("TRUE");
        omsTargetCategoryExtend.setUpdateStatus("NEW");
        omsTargetCategoryExtend.setArchivngType("NOT_ARCHIVE");
        omsTargetCategoryExtend.setIsEditing("TRUE");
        omsTargetCategoryExtend.setIsArchive("FALSE");
        omsTargetCategoryExtend.setSubjectLifeCycle("EDITING");
        omsTargetCategoryExtend.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCategoryExtend.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCategoryExtend.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetCategoryExtend.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetCategoryExtend.setIsValid("FALSE");
        omsTargetCategoryExtend.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsTargetCategoryExtend.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsTargetCategoryExtend)) {
            return omsTargetCategoryExtend.getObjectivePlanningExtendId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-3-02查询目标分类扩展设置列表[3646]
     * gen by moon at 10/15/2022, 10:05:57 PM
     */
    @Trace(operationName = "3-3-02查询目标分类扩展设置列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCategoryExtend> queryTargetCatExSetList(QueryTargetCatExSetListReq reqDto) {

        try {
            QueryWrapper<OmsTargetCategoryExtend> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCategoryExtend.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCategoryExtend.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCategoryExtend.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCategoryExtend>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-02批量改目标分类扩展设置[3647]
     * gen by moon at 10/15/2022, 10:05:58 PM
     */
    @Trace(operationName = "3-3-02批量改目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateTargetCatExSet(List<OmsTargetCategoryExtend> omsTargetCategoryExtend) {

        if (omsTargetCategoryExtend == null || omsTargetCategoryExtend.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsTargetCategoryExtend);

    }

    /**
     * 3-3-02批量删目标分类扩展设置[3648]
     * gen by moon at 10/15/2022, 10:05:59 PM
     */
    @Trace(operationName = "3-3-02批量删目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteTargetCatExSet(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-3-02-01更新目标分类扩展设置[3688]
     * gen by moon at 10/15/2022, 10:05:59 PM
     */
    @Trace(operationName = "3-3-02-01更新目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean refreshTargetCatExSet(OmsTargetCategoryExtend omsTargetCategoryExtend) {

        return super.updateById(omsTargetCategoryExtend);
    }

    /**
     * M3-构造单条目标分类扩展设置验证数据[4330]
     * gen by moon at 10/15/2022, 10:06:00 PM
     */
    @Trace(operationName = "M3-构造单条目标分类扩展设置验证数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String buildSingleTargetCatExtend(OmsTargetCategoryExtend omsTargetCategoryExtend) {
        omsTargetCategoryExtend.setObjectivePlanningExtendId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTargetCategoryExtend.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCategoryExtend.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsTargetCategoryExtend != null && omsTargetCategoryExtend.getSubjectLifeCycle() != null && omsTargetCategoryExtend.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetCategoryExtend.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsTargetCategoryExtend != null && omsTargetCategoryExtend.getSubjectLifeCycle() != null && omsTargetCategoryExtend.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsTargetCategoryExtend.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsTargetCategoryExtend.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetCategoryExtend.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsTargetCategoryExtend != null && omsTargetCategoryExtend.getSubjectLifeCycle() != null && omsTargetCategoryExtend.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetCategoryExtend.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsTargetCategoryExtend != null && omsTargetCategoryExtend.getSubjectLifeCycle() != null && omsTargetCategoryExtend.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsTargetCategoryExtend.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsTargetCategoryExtend.setIsValid("FALSE");
        omsTargetCategoryExtend.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsTargetCategoryExtend.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsTargetCategoryExtend)) {
            return omsTargetCategoryExtend.getObjectivePlanningExtendId();
        } else {
            return "-1";
        }

    }

    /**
     * M3-查询目标分类扩展设置列表[4370]
     * gen by moon at 4/15/2023, 1:10:29 AM
     */
    @Trace(operationName = "M3-查询目标分类扩展设置列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCategoryExtend> queryTargetCatExtendList(QueryTargetCatExtendListReq reqDto) {

        try {
            QueryWrapper<OmsTargetCategoryExtend> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCategoryExtend.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCategoryExtend.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCategoryExtend.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCategoryExtend>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M3-批量删目标分类扩展设置[4371]
     * gen by moon at 10/15/2022, 10:06:01 PM
     */
    @Trace(operationName = "M3-批量删目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteTargetCatExtend(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M3-3-02-01删除单条目标分类扩展设置[4513]
     * gen by moon at 10/15/2022, 10:06:02 PM
     */
    @Trace(operationName = "M3-3-02-01删除单条目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSingleTargetCatExtend(String objectivePlanningExtendId) {

        return super.removeById(objectivePlanningExtendId);
    }

    /**
     * M3-3-02-01查询单条目标分类扩展设置详情[4514]
     * gen by moon at 4/15/2023, 9:03:24 PM
     */
    @Trace(operationName = "M3-3-02-01查询单条目标分类扩展设置详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTargetCategoryExtend querySingleTargetCatExtendDetail(QuerySingleTargetCatExtendDetailReq reqDto) {

        try {

            QueryWrapper<OmsTargetCategoryExtend> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsTargetCategoryExtend> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsTargetCategoryExtend retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-02-01修改目标分类扩展设置[7647]
     * gen by moon at 11/1/2023, 9:10:16 PM
     */
    @Trace(operationName = "3-3-02-01修改目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTargetCatExSet(OmsTargetCategoryExtend omsTargetCategoryExtend) {

        return super.updateById(omsTargetCategoryExtend);
    }

    /**
     * 3-3-02-01批量查询目标分类扩展设置[7685]
     * gen by moon at 11/4/2023, 3:11:01 PM
     */
    @Trace(operationName = "3-3-02-01批量查询目标分类扩展设置")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCategoryExtend> batchQueryTargetCatExSet(BatchQueryTargetCatExSetReq reqDto) {

        try {
            QueryWrapper<OmsTargetCategoryExtend> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getTargetCatExSetList() == null || reqDto.getTargetCatExSetList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("target_category_id", reqDto.getTargetCatExSetList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCategoryExtend.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCategoryExtend.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCategoryExtend.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCategoryExtend>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

}

